/*
 * @Description  : Cookie
 * @Autor        : 杜康
 * @Date         : 2021-08-13 23:39:05 +0800
 * @LastEditors  : 杜康
 * @LastEditTime : 2021-08-14 10:19:10 +0800
 */

/**
 * @description: 获取Cookie
 * @param {*} cname
 * @return {*}
 * @author: 杜康
 * @Date: 2021-08-14 05:59:48 +0800
 */
const getCookie = (cname) => {
    let name = cname + "=";
    let ca = document.cookie.split(";");
    for (let i = 0; i < ca.length; i++) {
        let c = ca[i].trim();
        if (c.indexOf(name) == 0) return c.substring(name.length, c.length);
    }
    return "";
};

/**
 * @description: 设置Cookie
 * @param {*} cname
 * @param {*} cvalue
 * @param {*} exdays
 * @return {*}
 * @author: 杜康
 * @Date: 2021-08-14 06:00:24 +0800
 */
const setCookie = (cname, cvalue, exdays) => {
    let d = new Date();
    d.setTime(d.getTime() + exdays * 24 * 60 * 60 * 1000);
    let expires = "expires=" + d.toGMTString();
    document.cookie = cname + "=" + cvalue + "; " + expires;
};

/**
 * @description: 删除Cookie
 * @param {*} cname
 * @return {*}
 * @author: 杜康
 * @Date: 2021-08-14 06:00:45 +0800
 */
const removeCookie = (cname) => {
    setCookie(cname, "", -1); //cookie的过期时间设为昨天
};

export default { setCookie, getCookie, removeCookie };
